package com.prg.service;

import com.prg.dao.base.BaseDaoI;
import com.prg.model.base.AuthMenu;
import com.prg.model.easyui.Grid;
import com.prg.model.easyui.Tree;
import com.prg.service.impl.BaseServiceImpl;
import com.prg.util.HqlFilter;
import com.prg.util.JsonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-1-2
 * Time: 下午3:44
 * To change this template use File | Settings | File Templates.
 */
@Service
public class LeftTreeService extends BaseServiceImpl<AuthMenu>{
    @Autowired
    private BaseDaoI dao;

    /**
     * 异步的得到左侧树
     * @param pid
     * @return
     */
    public List getLiftTree(String pid){
        List<AuthMenu> menuList=new ArrayList();
        List treeList=new ArrayList();
        if (null==pid){
            //得到根节点
            String hql="from AuthMenu t where t.authMenu is null";
            menuList=dao.find(hql);
            for(AuthMenu menu:menuList){
                Tree tree=new Tree();
                tree.setId(menu.getId());
                tree.setText(menu.getName());
                tree.setState("closed");
                treeList.add(tree);

            }
            ;

        }else{
            Map<String,Object> params=new HashMap<String,Object>();
            params.put("pid",pid);
            String hql="from AuthMenu t where t.authMenu.id = :pid";
            menuList= dao.find(hql,params);
            for (AuthMenu menu:menuList){
                Tree tree=new Tree();
                tree.setId(menu.getId());
                tree.setState("closed");
                tree.setText(menu.getName());
                treeList.add(tree);
            }
        }

        return treeList;

    }

    /**
     * 得到左侧树 非异步
     * @return
     */
    public List getLiftTreeAll(){
        List<AuthMenu> menuList=new ArrayList();
        List<Tree> treeList=new ArrayList();
        String hql="from AuthMenu t";
        menuList= dao.find(hql);
        for(AuthMenu e:menuList){
            Map<String,String> attributes=new HashMap<String, String>();
            Tree node=new Tree();
            BeanUtils.copyProperties(e,node);
            node.setText(e.getName());
            if(null!=e.getAuthMenu()){
                node.setPid(e.getAuthMenu().getId());
            }
            if(null!=e.getUrl()){

                attributes.put("url",e.getUrl());
            }


            node.setAttributes(attributes);
            treeList.add(node);

        }

      return treeList;
    }

    public void getAuthMenu(HttpServletRequest request,HttpServletResponse response,BaseServiceImpl service ){
        Grid grid=new Grid();
        HqlFilter hqlFilter=new HqlFilter(request);
        grid.setTotal(service.countByFilter(hqlFilter));
        List<AuthMenu> menuList=new ArrayList();
        List<AuthMenu> tempList=new ArrayList<AuthMenu>();
        menuList=service.findByFilter(hqlFilter);

        for(AuthMenu e:menuList){
             AuthMenu temp=e ;
            if(temp.getAuthMenu()!=null){
                temp.setPid(e.getAuthMenu().getId());
                temp.setAuthMenu(null);
            }
            temp.setAuthMenus(null);
            tempList.add(temp);
        }
        grid.setRows(tempList);
        JsonUtil.writeJson(response,grid);
    }
    /**
     * 异步的得到菜单管理页面
     * @param pid
     * @return
     */
    public List getMenuTreeGrid(String pid){
        List<AuthMenu> menuList=new ArrayList();
        List<AuthMenu> treeList=new ArrayList();
        String hql="from AuthMenu t";
        menuList=dao.find(hql);

        for (AuthMenu e:menuList){
           // AuthMenu node=new AuthMenu() ;
         //   BeanUtils.copyProperties(e,node);
            AuthMenu node=e;
             if(null!=e.getAuthMenu()){
                node.setPid(e.getAuthMenu().getId());

             }
            node.setAuthMenu(null);
            node.setAuthMenus(null);
            treeList.add(node);
        }
        return treeList;
    }
    /**
    *update authmenu
    **/
    public void updateMenu(AuthMenu menu){
        AuthMenu p=new AuthMenu();
        if (menu.getPid()!=null){
            p= (AuthMenu) dao.getById(AuthMenu.class,menu.getPid());
            menu.setAuthMenu(p);
        }

        dao.update(menu);
    }
    /**
     *add new authmenu
     **/
    public void saveMenu(AuthMenu menu){
        AuthMenu p=new AuthMenu();
        if (menu.getPid()!=null){
            p= (AuthMenu) dao.getById(AuthMenu.class,menu.getPid());
            menu.setAuthMenu(p);
        }

        dao.save(menu);
    }

}
